<?php

use core\Menu;

/**
 * 模块信息
 */
$module_info = [
	'version' => '1.0.0',
	'title' => '积分',
	'description' => '',
	'url' => '',
	'email' => '68103403@qq.com',
	'author' => 'sunkangchina'
];


Menu::setGroup('admin');
Menu::add('source', '资源', '', 'bi-suit-club', 900);
Menu::add('point', '积分', '/point/admin', 'bi-coin', 80, 'source');
Menu::add('point-product', '积分商品', '/point/product', 'bi-coin', 90, 'source');
Menu::add('point-order', '积分订单', '/point/order', 'bi-coin', 91, 'source');


/**
 * 增加用户积分
 * @param int $user_id 用户ID
 * @param int $point 积分
 * @param string $type 类型
 * @param string $title 标题
 * @param string $desc 描述
 * @return void 
 */
function add_point($user_id, $point, $type = 'order', $title, $desc = '')
{
	global $uid;
	$admin_id = $uid ?: 0;
	$data = [
		'user_id' => $user_id,
		'point' => $point,
		'title' => $title,
		'type' => $type,
		'desc' => $desc,
		'created_at' => time(),
		'admin_id' => $admin_id,
	];
	db_insert('point_his', $data);
	$user_point = db_get_one('point', '*', ['user_id' => $user_id]);
	if (!$user_point) {
		db_insert("point", [
			'user_id' => $user_id,
			'point' => $point,
		]);
	} else {
		db_update('point', ['point' => $user_point['point'] + $point], ['user_id' => $user_id]);
	}
}
